﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FamilyTree_v1._0
{
    public partial class frmAddPerson : Form
    {
        private DatabaseHelper db;
        private Person personInfor;
        public delegate void EventSave();
        public EventSave function_EventSave;
        public frmAddPerson()
        {
            InitializeComponent();
            db = new DatabaseHelper();
            personInfor = new Person();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            function_EventSave();
        }
        public void addMember()
        {
            getInfor();
            db.AddPerson(personInfor);
        }
        public void editMember()
        {
            getInfor();
            db.UpdatePerson(personInfor);
        }
        public Person getPersonInfor()
        {
            return personInfor;
        }
        public void setPersonInfor(Person personInfor)
        {
            this.personInfor = personInfor;
            setInfor();
        }
        public void getInfor()
        {
            personInfor.FirstName = this.textFirstName.Text.Trim();
            personInfor.MiddleName = this.textMiddleName.Text.Trim();
            personInfor.LastName = textLastName.Text.Trim();
            personInfor.OtherName = textOtherName.Text.Trim();
            personInfor.DateOfBirth = dateDateOfBirth.Value;
            personInfor.PlaceOfBirth = textPlaceOfBirth.Text.Trim();
            if (checkDeath.Checked)
            {
                personInfor.DateOfDeath = dateDateOfDeath.Value;
                personInfor.PlaceOfDeath = textPlaceOfDeath.Text.Trim();
            }
            if (radioMale.Checked)
            {
                personInfor.Sex = true;
            }
            if (radioFemale.Checked)
            {
                personInfor.Sex = false;
            }
        }
        public void setMale()
        {
            radioMale.Checked = true;
            radioFemale.Enabled = false;
            radioUnknow.Enabled = false;
        }
        public void setFemale()
        {
            radioFemale.Checked = true;
            radioMale.Enabled = false;
            radioUnknow.Enabled = false;
        }
        public void setInfor()
        {
            if (personInfor != null)
            {
                this.textFirstName.Text = personInfor.FirstName;
                this.textMiddleName.Text = personInfor.MiddleName;
                this.textLastName.Text = personInfor.LastName;
                this.textOtherName.Text = personInfor.OtherName;
                this.dateDateOfBirth.Value = personInfor.DateOfBirth.Value;
                this.textPlaceOfBirth.Text = personInfor.PlaceOfBirth;
                if (personInfor.Sex == true)
                {
                    this.radioMale.Checked = true;
                }
                else
                {
                    this.radioFemale.Checked = true;
                }
            }

        }
        private void checkDead_CheckedChanged(object sender, EventArgs e)
        {
            if (checkDeath.Checked)
            {
                labelDeath.Visible = true;
                dateDateOfDeath.Visible = true;
                labelPlaceOfDeath.Visible = true;
                textPlaceOfDeath.Visible = true;
            }
            if (!checkDeath.Checked)
            {
                labelDeath.Visible = false;
                dateDateOfDeath.Visible = false;
                labelPlaceOfDeath.Visible = false;
                textPlaceOfDeath.Visible = false;
            }
        }
    }
}
